package Control;

import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JOptionPane;

import com.google.gdata.data.extensions.Email;

import Utils.MailHandler;

import Entity.Customer;
import Entity.E_Mail;
import Entity.MsgPackage;
import Entity.Schedule;
import Entity.User;
import Entity.WorkerEmails;
import GUI.EditShifts;
import GUI.ViewShifts;
import GUI.ScreenManager;
import GUI.SetShiftGui;
import GUI.shiftsArrangement;
/**
 * This class handle the worker schedule of the company and send the given information from the GUI's class to the server
 * it's create an event, cancel event, edit event and close an event
 * @author aviat
 */
public class HandleSchedule {

	private SetShiftGui setShiftGui; 
	private ViewShifts scheduleGui;
	private shiftsArrangement managerShiftsArrangementGui; 
	private EditShifts editShiftGui;
	private int rowNum=0;
	private MsgPackage hendlerScheduleMsg;
	private Schedule s;
	/**
	 * Constructor
	 */
	public HandleSchedule() {
		scheduleGui = new ViewShifts();
		setShiftGui = new SetShiftGui();
		managerShiftsArrangementGui = new shiftsArrangement();
		editShiftGui = new EditShifts();
		hendlerScheduleMsg = new MsgPackage();
		s = new Schedule();
	}

	/**
	 * This method send a request to server to get all users from DB 
	 *@return an array list that contain all customers of the company
	 */
	public ArrayList<User> getAllUsers() {
	System.out.println("in all users");
		hendlerScheduleMsg.setObject(editShiftGui.getDatecomboBox().getSelectedItem().toString());
		hendlerScheduleMsg.setOp_code(OP_CODE.GET_WORKER_TO_SCHEDULE);
		ScreenManager.getConnection().accept(hendlerScheduleMsg);
		
		Object o = ScreenManager.getConnection().cclient.getMessage();
		System.out.println("back to all users");
		return (ArrayList<User>)o;
		
	}
	/**
	 * This method set a request to server to get the event's from DB to set them schedule 
	 * @return a list with the events that needs to be schedule
	 */
	public ArrayList<Schedule> getEventToSched(){
		
		hendlerScheduleMsg.setOp_code(OP_CODE.GET_EVENT_TO_SCHEDULE);
		ScreenManager.getConnection().accept(hendlerScheduleMsg);
		
		Object o = ScreenManager.getConnection().cclient.getMessage();
		return (ArrayList<Schedule>) o;
	}
	/**
	 * This method set a request to server to get the working schedule and prepare it to display
	 */
	public void getSchedule(){
		
		hendlerScheduleMsg.setOp_code(OP_CODE.VIEW_SCHEDULE);
		ScreenManager.getConnection().accept(hendlerScheduleMsg);
		
		Object o = ScreenManager.getConnection().cclient.getMessage();
		ArrayList<Schedule> sched = (ArrayList<Schedule>) o;
		Iterator<Schedule> it = sched.iterator();
		while(it.hasNext()){
			Schedule s = it.next();
			scheduleGui.getTable().setValueAt(s.getDate(), rowNum, 0);
			scheduleGui.getTable().setValueAt(s.getPlace(), rowNum, 1);
			scheduleGui.getTable().setValueAt(s.getTeamLeader(), rowNum, 2);
			scheduleGui.getTable().setValueAt(s.getStilsPhoto(), rowNum, 3);
			scheduleGui.getTable().setValueAt(s.getNumOfVideoPhoto(), rowNum,4);
			scheduleGui.getTable().setValueAt(s.getvPhotografer1(), rowNum, 5);
			scheduleGui.getTable().setValueAt(s.getvPhotografer2(), rowNum, 6);
			scheduleGui.getTable().setValueAt(s.isHoverCam(), rowNum, 7);
			scheduleGui.getTable().setValueAt(s.isMagnets(), rowNum, 8);
			scheduleGui.getTable().setValueAt(s.getMagnetsPhoto1(), rowNum, 9);
			scheduleGui.getTable().setValueAt(s.getMagnetsPhoto2(), rowNum, 10);
			scheduleGui.getTable().setValueAt(s.getComments(), rowNum, 11);
			scheduleGui.getTable().setValueAt(s.getgContactName(), rowNum, 12);
			scheduleGui.getTable().setValueAt(s.getgContactPhone(), rowNum, 13);
			scheduleGui.getTable().setCellSelectionEnabled(false);
			rowNum++;
		}
	}
	/**
	 * This method set a request to server to set the working schedule in DB
	 */
	public void addToSchedule(){
		
		
		s.setPlace(editShiftGui.getPlacecomboBox().getSelectedItem().toString());
		s.setDate(editShiftGui.getDatecomboBox().getSelectedItem().toString());
		s.setbContactName(editShiftGui.getContactNametextField().getText().toString());
		s.setbContactPhone(editShiftGui.getContactPhonetextField().getText().toString());
		s.setStilsPhoto(setShiftGui.getStillsComboBox().getSelectedItem().toString());
		s.setTeamLeader(setShiftGui.getTeamLeadercomboBox().getSelectedItem().toString());
		
		if(setShiftGui.getRadioButton1().isSelected()){
			s.setNumOfVideoPhoto(1);
			s.setvPhotografer1(setShiftGui.getVideo1ComboBox().getSelectedItem().toString());
		}
		else if(setShiftGui.getRadioButton2().isSelected()){
			s.setNumOfVideoPhoto(2);
			s.setvPhotografer1(setShiftGui.getVideo1ComboBox().getSelectedItem().toString());
			s.setvPhotografer2(setShiftGui.getVideo2comboBox().getSelectedItem().toString());
		}
		
		if(setShiftGui.getChckbxHoverCamera().isSelected())
			s.setHoverCam("YES");
		if(setShiftGui.getChckbxMagnets().isSelected()){
			s.setMagnets("YES");
			s.setMagnetsPhoto1(setShiftGui.getMagnets1comboBox().getSelectedItem().toString());
			s.setMagnetsPhoto2(setShiftGui.getMagnets2ComboBox().getSelectedItem().toString());
		}
		if(!(setShiftGui.getCommentTextPane().getText().toString().equals("")))
			s.setComments(setShiftGui.getCommentTextPane().getText().toString());
			
		hendlerScheduleMsg.setObject(s);
		hendlerScheduleMsg.setOp_code(OP_CODE.SET_SCHEDULE);
		ScreenManager.getConnection().accept(hendlerScheduleMsg);
		
		Object o = ScreenManager.getConnection().cclient.getMessage();
		if((boolean)o)
			JOptionPane.showMessageDialog(null, "schedule for event date: "+s.getDate()+" at "+s.getPlace()+" was set successfully", "", JOptionPane.INFORMATION_MESSAGE);
		
		sendConfirmationMail();
		setShiftGui.getCommentTextPane().setText("");
	}

	/**
	 * This method send a request to server to sent an email to manager and customer with the new meeting datails 
	 * or to notify manager abut changes
	 * @return true if e-mails where sent successfully and false otherwuse
	 */
	private void sendConfirmationMail() {
		
		WorkerEmails wEmail = new WorkerEmails();
		
		wEmail.setS_name(s.getStilsPhoto());
		wEmail.setV1_name(s.getvPhotografer1());
		wEmail.setTeam_name(s.getTeamLeader());
		if(s.isMagnets().equals("YES")){
			wEmail.setM1_name(s.getMagnetsPhoto1());
			wEmail.setM2_name(s.getMagnetsPhoto2());
		}
		if(s.getNumOfVideoPhoto()==2)
			wEmail.setV2_name(s.getvPhotografer2());
		
		hendlerScheduleMsg.setObject(wEmail);
		hendlerScheduleMsg.setOp_code(OP_CODE.GET_WORKER_EMAIL);
		ScreenManager.getConnection().accept(hendlerScheduleMsg);

		Object o2 = ScreenManager.getConnection().cclient.getMessage();
		wEmail = (WorkerEmails) o2;
	
		MailHandler.generateShiftMailForWorker(wEmail.getS_mail());
		MailHandler.generateShiftMailForWorker(wEmail.getV1_mail());
		MailHandler.generateShiftMailForWorker(wEmail.getTeam_mail());
		if(s.isMagnets().equals("YES")){
			MailHandler.generateShiftMailForWorker(wEmail.getM1_mail());
			MailHandler.generateShiftMailForWorker(wEmail.getM2_mail());
		}
		if(s.getNumOfVideoPhoto()==2)
			MailHandler.generateShiftMailForWorker(wEmail.getV2_mail());

		JOptionPane.showMessageDialog(null, "Confirmation mail was sent to all worker rellevants ", "", JOptionPane.INFORMATION_MESSAGE);
			
	}
		
	/**
	 * This method will clear the text, combobox and button from editShiftGui from it's older selection
	 */
	public void clearText(){
		editShiftGui.getContactNametextField().setText("");
		editShiftGui.getContactPhonetextField().setText("");
		editShiftGui.getDatecomboBox().removeAllItems();
		editShiftGui.getPlacecomboBox().removeAllItems();
		editShiftGui.getRdbtnContactBride().setSelected(false);
		editShiftGui.getRdbtnContactGroom().setSelected(false);
	}
		
	/**
	 * @return the scheduleGui
	 */
	public ViewShifts getScheduleGui() {
		return scheduleGui;
	}

	/**
	 * @return the shiftsArrangementGui
	 */
	public shiftsArrangement getManagerShiftsArrangementGui() {
		return managerShiftsArrangementGui;
	}

	/**
	 * @return the editShiftGui
	 */
	public EditShifts getEditShiftGui() {
		return editShiftGui;
	}

	/**
	 * @return the setShiftGui
	 */
	public SetShiftGui getSetShiftGui() {
		return setShiftGui;
	}

	

}
